package com.gibs.peixunapi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.utils.ResultUtil;
import com.gibs.peixunapi.dao.CommentDao;
import com.gibs.peixunapi.service.CommentService;
import com.gibs.peixunapi.model.Comment;

@Service
@Slf4j
public class CommentServiceImpl implements CommentService{

	@Autowired
	private CommentDao commentDao;

	@Transactional
@Override
	public Result create(@Valid Comment object) {
		try {
		
			commentDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(@Valid Comment object) {
		try {
			
//			Comment comment = commentDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,comment);
//			commentDao.save(comment);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				commentDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
			HashMap<String, Object> data = new HashMap<String, Object>();
			Comment object = commentDao.getOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
//			HashMap<String,Object> map = new HashMap();
//			map.put("id", object.getId());
//			HashMap<String,Object> videoClassId = new HashMap<String,Object>();
//			videoClassId.put("name", object.getVideoClassId()==null?"":object.getVideoClassId().getName());
//			videoClassId.put("id", object.getVideoClassId()==null?"":object.getVideoClassId().getId());
//			map.put("videoClassId", videoClassId);
//			HashMap<String,Object> documentId = new HashMap<String,Object>();
//			documentId.put("name", object.getDocumentId()==null?"":object.getDocumentId().getName());
//			documentId.put("id", object.getDocumentId()==null?"":object.getDocumentId().getId());
//			map.put("documentId", documentId);
//			HashMap<String,Object> creatorId = new HashMap<String,Object>();
//			creatorId.put("name", object.getCreatorId()==null?"":object.getCreatorId().getName());
//			creatorId.put("id", object.getCreatorId()==null?"":object.getCreatorId().getId());
//			map.put("creatorId", creatorId);
//			map.put("content", object.getContent()==null?"":object.getContent());
//			map.put("type", object.getType()==null?"":object.getType());
//			HashMap<String,Object> accepter = new HashMap<String,Object>();
//			accepter.put("name", object.getAccepter()==null?"":object.getAccepter().getName());
//			accepter.put("id", object.getAccepter()==null?"":object.getAccepter().getId());
//			map.put("accepter", accepter);
//			HashMap<String,Object> isDelete = new HashMap<String,Object>();
//			isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//			isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//			map.put("isDelete", isDelete);
//			data.put("object",map);

			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
			List<Comment> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = commentDao.findLike("%" + key + "%");
			} else {
				list = commentDao.findAll();
			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Comment object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				HashMap<String,Object> videoClassId = new HashMap<String,Object>();
//				videoClassId.put("name", object.getVideoClassId()==null?"":object.getVideoClassId().getName());
//				videoClassId.put("id", object.getVideoClassId()==null?"":object.getVideoClassId().getId());
//				map.put("videoClassId", videoClassId);
//				HashMap<String,Object> documentId = new HashMap<String,Object>();
//				documentId.put("name", object.getDocumentId()==null?"":object.getDocumentId().getName());
//				documentId.put("id", object.getDocumentId()==null?"":object.getDocumentId().getId());
//				map.put("documentId", documentId);
//				HashMap<String,Object> creatorId = new HashMap<String,Object>();
//				creatorId.put("name", object.getCreatorId()==null?"":object.getCreatorId().getName());
//				creatorId.put("id", object.getCreatorId()==null?"":object.getCreatorId().getId());
//				map.put("creatorId", creatorId);
//				map.put("content", object.getContent()==null?"":object.getContent());
//				map.put("type", object.getType()==null?"":object.getType());
//				HashMap<String,Object> accepter = new HashMap<String,Object>();
//				accepter.put("name", object.getAccepter()==null?"":object.getAccepter().getName());
//				accepter.put("id", object.getAccepter()==null?"":object.getAccepter().getId());
//				map.put("accepter", accepter);
//				HashMap<String,Object> isDelete = new HashMap<String,Object>();
//				isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//				isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//				map.put("isDelete", isDelete);
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
			List<Comment> list = new ArrayList<>();
			limit = limit == null ? 10 : limit;
			current = current == null ? 1 : current;
			int offset = (current - 1) * limit;
			Long total = null;
			if (!StringUtils.isEmpty(key)) {
				list = commentDao.findPageLike("%" + key + "%", offset, limit);
				total = commentDao.countLike("%" + key + "%");
			} else {
				list = commentDao.findPage(offset, limit);
				total = commentDao.count();
			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Comment object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				HashMap<String,Object> videoClassId = new HashMap<String,Object>();
//				videoClassId.put("name", object.getVideoClassId()==null?"":object.getVideoClassId().getName());
//				videoClassId.put("id", object.getVideoClassId()==null?"":object.getVideoClassId().getId());
//				map.put("videoClassId", videoClassId);
//				HashMap<String,Object> documentId = new HashMap<String,Object>();
//				documentId.put("name", object.getDocumentId()==null?"":object.getDocumentId().getName());
//				documentId.put("id", object.getDocumentId()==null?"":object.getDocumentId().getId());
//				map.put("documentId", documentId);
//				HashMap<String,Object> creatorId = new HashMap<String,Object>();
//				creatorId.put("name", object.getCreatorId()==null?"":object.getCreatorId().getName());
//				creatorId.put("id", object.getCreatorId()==null?"":object.getCreatorId().getId());
//				map.put("creatorId", creatorId);
//				map.put("content", object.getContent()==null?"":object.getContent());
//				map.put("type", object.getType()==null?"":object.getType());
//				HashMap<String,Object> accepter = new HashMap<String,Object>();
//				accepter.put("name", object.getAccepter()==null?"":object.getAccepter().getName());
//				accepter.put("id", object.getAccepter()==null?"":object.getAccepter().getId());
//				map.put("accepter", accepter);
//				HashMap<String,Object> isDelete = new HashMap<String,Object>();
//				isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//				isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//				map.put("isDelete", isDelete);
//				data.add(map);
//			}
			PageData pageData = new PageData(0, limit, current);
			return ResultUtil.successPage(ResultEnum.SUCCESS, pageData);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
			List<Comment> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = commentDao.findLike("%" + key + "%");
			} else {
				list = commentDao.findAll();
			}
			List<Object> data = new ArrayList<>();
			for (Comment object : list) {
				HashMap<String, Object> map = new HashMap();
				map.put("value", object.getId());
				map.put("name", object.getContent());
				data.add(map);
			}
			return ResultUtil.success(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


}

